﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorkStation.Frameworks.Core;
using WorkStation.Frameworks.Core.Extensions;
using WorkStation.Frameworks.EasyBus.Serializable;
using WorkStation.Frameworks.EasyBus.MessageQueue;

namespace WorkStation.Frameworks.EasyBus.Extensions
{
    public static class StringExtensions
    {
        public static IEndpoint ToEndpoint(this string str) 
        {
            return ServiceContainer.Current.GetAllInstances<IQueueAddressTranslator>().Select(v => v.CreateEndpoint(str)).First(v => v != null);
        }

        public static object JsonToObject(this string json, Type type)
        {
            return ServiceContainer.Current.GetInstance<IMessageSerializable>().JsonToObject(json, type);
        }

        public static T JsonToObject<T>(this string json) where T : class
        {
            return ServiceContainer.Current.GetInstance<IMessageSerializable>().JsonToObject(json, typeof(T)) as T;
        }
    }
}
